﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;
namespace DAO
{
    public class KhachHangDAO : DataProvider
    {
        public DataTable LayTableNoHoaDonCuaTungKhachHangTheoThang(string m)
        {
              string sql ="select hd.MaKhachHang,hd.MaHoaDOn,kh.TenKhachHang,sum(cthd.DonGiaHienTai) as TongGiaTriMoiHoaDon ";
              sql +=" from hoadon hd,chitiethoadon cthd,KhachHang kh ";
              sql +=" where  hd.mahoadon = cthd.mahoadon and ";
              sql +=" kh.MaKhachHang = hd.MaKhachHang and ";
              sql += " month(hd.ngaylap) = '"+m+"' ";
              sql += "group by hd.MaHoaDOn,hd.MaKhachHang,kh.TenKhachHang ";
            //sql += " month(hd.ngaylap = '"++"');
         
            SqlConnection con = DataProvider.ConnectDB();
            SqlCommand cmd = new SqlCommand(sql, con);
            con.Open();
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            return dt;

        }
        public DataTable LayTableTheoThang()
        {
            String sql = "SELECT * from KhachHang ";
            SqlConnection con = DataProvider.ConnectDB();

            SqlCommand command = new SqlCommand(sql, con);
            con.Open();
            SqlDataAdapter adapter = new SqlDataAdapter(command);

            DataTable dt = new DataTable();
            adapter.Fill(dt);

            con.Close();
            return dt;
        }
        public DataTable LayTable()
        {
            String sql = "SELECT * from KhachHang ";
            SqlConnection con = DataProvider.ConnectDB();

            SqlCommand command = new SqlCommand(sql, con);
            con.Open();
            SqlDataAdapter adapter = new SqlDataAdapter(command);

            DataTable dt = new DataTable();
            adapter.Fill(dt);

            con.Close();
            return dt;
        }
        public void Xoa(KhachHangDTO kh)
        {
            String sql = "DELETE from KhachHang  where MaKhachHang =" + kh.MaKhachHang;
            SqlConnection con = DataProvider.ConnectDB();
            con.Open();
            SqlCommand command = new SqlCommand(sql, con);
            int res = -1;
            res = command.ExecuteNonQuery();
            con.Close();
        }
    }
}
